package com.kadron.wall;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.kadron.common.KUtil;
import com.kadron.common.KeyValue;
import com.kadron.creator.KCommentModelCreator;
import com.kadron.dao.KCommonDAO;
import com.kadron.entity.Comment;
import com.kadron.paging.AbstractRowService;
import com.kadron.paging.KRowParent;
import com.kadron.paging.KRowService;

public class KCommentRowService extends AbstractRowService implements KRowService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5862117516679924629L;
	
	public KCommentRowService(KCommentModelCreator creator, KCommonDAO commonDAO) {
		super(creator, Comment.class, commonDAO);
	}

	public void getAllModels(KRowParent parent) throws UnsupportedOperationException {
		throw new UnsupportedOperationException();
	}

	public Query createQuery(boolean isCount) {
		EntityManager em = getCommonDAO().getEntityManager();
		Set<KeyValue> params = new HashSet<KeyValue>();
		StringBuilder builder = new StringBuilder("SELECT ");
		if (isCount) {			
			builder.append("COUNT(e.id) ");
		} else {			
			builder.append(" e.id ");
		}
		builder.append("FROM Comment e WHERE e.id > 0 AND e.approved=TRUE  ");
		params.add(new KeyValue("moment", KUtil.getMoment()));
		if (!isCount) {
			builder.append(" ORDER BY e.id DESC");
		}
		Query query = em.createQuery(builder.toString());
		for (KeyValue p : params) {
			query.setParameter(p.getKey(), p.getValue());
		}
		return query;
	}

}
